package vn.team.clipxonxao.respone;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.team.clipxonxao.model.Category;
import vn.team.clipxonxao.model.CategoryList;
import vn.team.clipxonxao.utils.ConfigApp;

import com.mk.fw.connection.APIDataResponse;
import com.mk.fw.connection.APIResponse;

public class CategoryRespone extends APIResponse {

	private CategoryList categoryList;
	private List<Category> list;

	public CategoryRespone(APIDataResponse dataResponse) {
		super(dataResponse);
	}

	@Override
	public void parseResponse(APIDataResponse response) {
		if (response == null) {
			return;
		}
		//Log.e("", "Data response=" + response.getData());
		try {
			JSONObject jsonObject = new JSONObject(response.getData());
			int code = jsonObject.getInt(ConfigApp.CODE);
			if (code == APIResponse.SERVER_SUCCESS) {

				categoryList = new CategoryList();
				list = new ArrayList<Category>();
				JSONArray jsonObject2 = jsonObject.getJSONArray(ConfigApp.DATA);
				for (int i = 0; i < jsonObject2.length(); i++) {
					JSONObject object = jsonObject2.getJSONObject(i);
					int id = object.getInt(ConfigApp.ID);
					String name = object.getString(ConfigApp.NAME);
					String des = object.getString(ConfigApp.DESCRIPTION);
					String thumbnail = object.getString(ConfigApp.THUMBNAIL);
					int status = object.getInt(ConfigApp.STATUS);

					Category category = new Category(id, name,"", des, thumbnail,status);
					list.add(category);

				}
				categoryList.setListCategory(list);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			setCode(APIResponse.CLIENT_ERROR_PARSE_DATA);
		}
	}

	public CategoryList getCategoryList() {
		return categoryList;
	}

}
